package com.example.myprojectoffeixue;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ASUS on 2016/10/20.
 */
public class KtGroupAdapter extends BaseAdapter {

    private ArrayList<String> d;
    private Context context;
    private KtGroupActivity Kt;
    private LayoutInflater layoutInflater;
    private HashMap<String, String> kt_map;
    public HashMap<String, Extra> kt2_map;
    public ArrayList<String> kt_flag;
    public boolean flag = false;

    public KtGroupAdapter(ArrayList<String> d, Context context, HashMap<String, String> kt_map, HashMap<String, Extra> kt2_map, ArrayList<String> kt_flag) {
        super();
        this.d = d;
        this.context = context;
        this.kt_map = kt_map;
        this.kt2_map = kt2_map;
        this.kt_flag = kt_flag;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder h = null;


        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_ktgroup, null);
            h.flag_kt = (ImageView) convertView.findViewById(R.id.iv_addcjsb);
            h.kt = (ImageView) convertView.findViewById(R.id.image_KT);
            h.name = (TextView) convertView.findViewById(R.id.name_item);
            h.all_Item = (LinearLayout) convertView.findViewById(R.id.All_item);
            h.current_Wd = (TextView) convertView.findViewById(R.id.current_item);
            h.Kt_Wd = (TextView) convertView.findViewById(R.id.kt_item);

            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }

        h.name.setText(kt_map.get(d.get(position)));

        if (kt2_map.containsKey(d.get(position))) {
            h.current_Wd.setText("室温：" + kt2_map.get(d.get(position)).roomTemperature);
            h.Kt_Wd.setText("设定：" + kt2_map.get(d.get(position)).temperature);
            if ("00".equals(kt2_map.get(d.get(position)).powerStatus)) {
                h.kt.setImageResource(R.drawable.icon_kt_unnormal);
                h.Kt_Wd.setTextColor(Color.parseColor("#898989"));
                h.current_Wd.setTextColor(Color.parseColor("#898989"));

            } else {
                h.kt.setImageResource(R.drawable.zykt);
                h.Kt_Wd.setTextColor(Color.parseColor("#76a6ef"));
                h.current_Wd.setTextColor(Color.parseColor("#76a6ef"));

            }


            if (flag) {
                h.flag_kt.setVisibility(View.VISIBLE);
            }

            if ("1".equals(kt_flag.get(position))) {
                h.flag_kt.setImageResource(R.drawable.button_kuang_yixuan);
            } else if ("0".equals(kt_flag.get(position))) {
                h.flag_kt.setImageResource(R.drawable.button_kuang_weixuan);
            }

            final ViewHolder finalH = h;
            h.flag_kt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("1".equals(kt_flag.get(position))) {
                        kt_flag.set(position, "0");
                        finalH.flag_kt.setImageResource(R.drawable.button_kuang_weixuan);
                    } else if ("0".equals(kt_flag.get(position))) {

                        kt_flag.set(position, "1");
                        finalH.flag_kt.setImageResource(R.drawable.button_kuang_yixuan);
                    }

                }
            });


        }
        return convertView;
    }

    class ViewHolder {

        TextView name;
        TextView current_Wd;
        TextView Kt_Wd;
        LinearLayout all_Item;
        ImageView flag_kt;
        ImageView kt;

    }


}
